/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package org.aroundme.persistency;

import org.aroundme.model.User;
import org.aroundme.utility.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;
/**
 * Persistency class responsible for saving and retrieving User-objects
 * from the DB
 * 
 */
public class UserManager {

	/**
	 * Saves a user object persistently to the DB
	 * @param user
	 */
	public void saveUser(User user){
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		s.beginTransaction();
		try{
			s.saveOrUpdate(user);
			s.getTransaction().commit();
		}catch (HibernateException e){
			s.getTransaction().rollback();
			throw e;
		}
	}
	
	/**
	 * Returns the user with the given loginName
	 * @param loginName
	 * @return User object representing the user with the String "loginName"
	 */
	public User getUserByLoginName(String loginName){
		User result = null;
		
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		s.beginTransaction();
		try{
			result = (User) s.createQuery("from User where loginName=?").setString(0, loginName).uniqueResult();
			s.getTransaction().commit();
		}catch (HibernateException e){
			s.getTransaction().rollback();
			throw e;
		}
		return result;
	}
}
